/**
 * Project Name:JZGPingGuShi
 * File Name:StringUtils.java
 * Package Name:com.gc.jzgpinggushi.uitls
 * Date:2014-10-8下午2:18:50
 * Copyright (c) 2014, wangyd523@gmail.com All Rights Reserved.
 *
 */

package com.jzg.jcptadmin.Utils;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

import com.jzg.jcptadmin.app.AppContext;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class StringUtils
{

	public static String StringData(){
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if("1".equals(mWay)){
			mWay ="天";
		}else if("2".equals(mWay)){
			mWay ="一";
		}else if("3".equals(mWay)){
			mWay ="二";
		}else if("4".equals(mWay)){
			mWay ="三";
		}else if("5".equals(mWay)){
			mWay ="四";
		}else if("6".equals(mWay)){
			mWay ="五";
		}else if("7".equals(mWay)){
			mWay ="六";
		}
		return mYear + "年" + mMonth + "月" + mDay+"日"+" 星期"+mWay;
	}

	public static void log(String tag,String msg){
		Log.d(tag, msg);
	}
	

	public static String getStringByUrlUTF8(String str)
			throws UnsupportedEncodingException
	{
		return URLEncoder.encode(str, "utf-8");
	}

	
	
	/**
	 * 2015-12  --> 2015
	 */
	public static int getYear4String(String yearStr) {
		int year = 0;
		try {
			if ("".equals(yearStr) || null == yearStr) {
				year = 0;
			} else {
				if (yearStr.indexOf("-") != -1) {
					year = Integer.parseInt(yearStr.substring(0,
							yearStr.indexOf("-")));
				} else {
					year = 0;
				}
			}
		} catch (Exception e) {
			year = 0;
		}
		return year;
	}

	public static enum TimeType{
		YEAR,MONTH,DAY;
	}
	public static int getCurrent(TimeType type){
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int result = 0;
		switch (type){
			case YEAR:
				result = calendar.get(Calendar.YEAR);
				break;
			case MONTH:
				result = calendar.get(Calendar.MONTH)+1;
				break;
			case DAY:
				result = calendar.get(Calendar.DAY_OF_MONTH);
				break;
		}
		return result;
	}
	
	
	/**
	 * 2015-03  --> 3
	 */
	public static int getMonth4String(String yearStr) {
		int year = 0;
		try {
			if ("".equals(yearStr) || null == yearStr) {
				year = 0;
			} else {
				String[] datas = yearStr.split("-");
				if(datas.length>0){
					year = Integer.valueOf(datas[1]);
				}else{
					year = 0;
				}
			}
		} catch (Exception e) {
			year = 0;
		}
		return year;
	}
	/**
	 * 2015-03-12  --> 12
	 */
	public static int getDay4String(String yearStr) {
		int year = 0;
		try {
			if ("".equals(yearStr) || null == yearStr) {
				year = 0;
			} else {
				String[] datas = yearStr.split("-");
				if(datas.length>1){
					year = Integer.valueOf(datas[2]);
				}else{
					year = 0;
				}
			}
		} catch (Exception e) {
			year = 0;
		}
		return year;
	}

	
	/**
	 * 北京市------》北京
	  * returnShi
	  *
	  * @Title: returnShi
	  * @Description: TODO
	  * @param @param shi
	  * @param @return    
	  * @return String
	  * @throws
	 */
	public static String returnShi(String shi){
		
		if(shi == null || "".equals(shi)){
			return "";
		}else{
			if(shi.indexOf("市")!=-1){
				return shi.substring(0,shi.indexOf("市"));
			}else{
				return shi;
			}
		}
		
		
	}
	public static String getPrice(String price){
		int C2B = 0;
		try {
			C2B = (int) (Float.parseFloat(price)*10000);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if(C2B>0.01){
			return String.valueOf(C2B);
		}else{
			return String.valueOf("");
		}

	}
	public static String getPrice1(String price){
		int C2B = 0;
		try {
			C2B = (int) (Float.parseFloat(price)*10000);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if(C2B>0.01){
			return price;
		}else{
			return String.valueOf("");
		}

	}
	public static int getIntPrice(String price){
		int C2B = 0;
		try {
			C2B = (int) (Float.parseFloat(price)*10000);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if(C2B>0.01){
			return C2B;
		}else{
			return 0;
		}

	}

	/**
	 * 验证价格,小数点前面三位,小数点后面两位
	 * @param editText
	 * @param context
     */
	public static void priceTextWatcher(final EditText editText, final AppContext context) {

		editText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
									  int count) {
				if (s.length() == 0) {
					return;
				}
				editText.setSelection(s.length());
				if (s.length() > 3 && !s.toString().contains(".")) {
					editText.setText(editText.getText().toString().subSequence(0,
							editText.getText().toString().length() - 1));
					editText.setSelection(editText.getText().toString().length());
					//Toast.makeText(context, "请输入小数点", Toast.LENGTH_SHORT).show();
					return;
				} else if (s.toString().contains(".")
						&& s.toString()
						.substring(s.toString().lastIndexOf("."))
						.length() > 3) {
					editText.setText(s.toString().substring(0,
							s.toString().lastIndexOf("."))
							+ s.toString()
							.substring(s.toString().lastIndexOf("."))
							.subSequence(0, 3));
					editText.setSelection(s.length() - 1);
					//Toast.makeText(context, "只能输入小数点后两位", Toast.LENGTH_SHORT).show();
					return;

				} else if ("0".equals(String.valueOf(s.charAt(0)))
						&& !s.toString().contains(".")) {
					if (s.length() == 2
							&& "0".equals(String.valueOf(s.charAt(0)))) {
						editText.setText("0");
						editText.setSelection(1);
					//	Toast.makeText(context, "请输入小数点", Toast.LENGTH_SHORT).show();
						return;
					}

				} else if (".".equals(String.valueOf(s.charAt(0)))) {
					editText.setText("");
					//Toast.makeText(context, "第一位不能为小数点", Toast.LENGTH_SHORT).show();

				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
										  int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 判断字符串日期的大小
	 * @param endTime  结束日期
	 * @param startTime  开始如期
     * @return
     */
	public static Boolean TimeCompare(String endTime,String startTime){
		//格式化时间
		SimpleDateFormat CurrentTime= new SimpleDateFormat("yyyy-MM-dd");
		try {

			Date overTime=CurrentTime.parse(endTime);
			Date beginTime=CurrentTime.parse(startTime);

			//判断是否大于两天
			if(((overTime.getTime() - beginTime.getTime()))>=0) {
				return true;
			}else{
				return false;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 郑有权
	 * @param douStr
	 * @return
	 */
	public static String  DoubleToString(String douStr){
		String rStr = "";
		if(TextUtils.isEmpty(douStr)){
			rStr = "";
		}else{
			DecimalFormat    df   = new DecimalFormat("######0.00");
			rStr = df.format(Double.parseDouble(douStr));
		}
		return rStr;
	}

    /**
     * 19231--->1.92
     * 郑有权
     * @param douStr
     * @return
     */
    public static String  DoubleToString(String douStr,double div){
        String rStr = "";
        if(douStr != null && !"".equals(douStr)){
            Double d = Double.parseDouble(douStr);
            BigDecimal b   =   new   BigDecimal(d/div);
            double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
            DecimalFormat    df   = new DecimalFormat("######0.00");
            rStr = df.format(f1);
        }
        return rStr;
    }


	/**
	 * 郑有权
	 * 1992-09-08----->1992年09月08日
	 * 1992-09---->1992年09月
	 * @param dataStr
	 * @return
	 */
	public static String formatData(String dataStr) {
		String s = "";
		if (dataStr != null && !"".equals(dataStr)) {
			SimpleDateFormat formatter;
			Date strtodate;
			DateFormat format2;
			try {

				if (dataStr.length() > 7) {
					formatter = new SimpleDateFormat("yyyy-MM-dd");
					strtodate = formatter.parse(dataStr);
					format2 = new SimpleDateFormat("yyyy年MM月dd日");
					s = format2.format(strtodate);
				}else{
					formatter = new SimpleDateFormat("yyyy-MM");
					strtodate = formatter.parse(dataStr);
					format2 = new SimpleDateFormat("yyyy年MM月");
					s = format2.format(strtodate);
				}

			} catch (ParseException e) {
//				e.printStackTrace();
				s = "";
			}

		}

		return s;
	}


}
